{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#  The Static and Shared Library with GCC\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##  The Library \n",
    "\n",
    "\n",
    "Library files are a collection of **precompiled functions** that are written according to the principle of reusability. \n",
    "\n",
    "\n",
    "### Static  Library \n",
    "\n",
    "Static libraries are **directly linked into** the program at compile time. \n",
    "\n",
    "A program using a static library takes copies of the code that it uses from the static library and makes it part of the program. \n",
    "\n",
    "\n",
    "### Shared  Library \n",
    "\n",
    "For a shared library, the compiler/linker checks that the names you link with exist in the library when the application is built, **but doesn't move their code into the application**. At run time, the shared library must be available.\n",
    "\n",
    "Before the executable starts running, **the operating system loads the machine code needed for the external functions** - a process known as **dynamic linking.** \n",
    "\n",
    "![library](./img/library.png) \n",
    "\n",
    "### The advantages and disadvantages in each method\n",
    "\n",
    "There are two powerful ideas for modular programming with C/C++ programs, \n",
    "\n",
    "* Modular organization of `sources`:  organize program sources into a source tree\n",
    "\n",
    "* Modular organization of `object files`:  combine object files into libraries\n",
    "\n",
    "**The advantages of using library**\n",
    "\n",
    "1. Modular development\n",
    "\n",
    "2. Reusability\n",
    "\n",
    "3. Maintainability\n",
    "\n",
    "**Shared libraries**\n",
    "\n",
    "* reduce the amount of code that is duplicated in each program that makes use of the library, keeping **binaries small**. \n",
    "\n",
    "* allows you to replace the shared object with one that is functionally equivalent, but may have added performance benefits **without needing to recompile** the program that makes use of it. \n",
    "\n",
    "Shared libraries will, however have a small additional cost for the execution of the functions as well as a run-time loading cost as all the symbols in the library need to be connected to the things they use\n",
    "\n",
    "**Static libraries**\n",
    "\n",
    "* increase the overall size of the binary, but it means that you don't need to carry along a copy of the library that is being used. As the code is connected at compile time there are not any additional run-time loading costs. The code is simply there.\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "**GCC assumes that all libraries**\n",
    "   \n",
    "* `start` with **lib**\n",
    "\n",
    "* Static libraries: `end` with `.a`\n",
    "\n",
    "* Shared libraries: `end`  with `.dll`(windows) or `.so`(Linux)，\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Change the current working directory to `./demo`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "J:\\SEU\\SEE\\PySEE\\home\\notebook\\demo\n"
     ]
    }
   ],
   "source": [
    "%cd demo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'J:\\\\SEU\\\\SEE\\\\PySEE\\\\home\\\\notebook'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%pwd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "## 1 The shared library\n",
    "\n",
    "### 1.1 The source code of library \n",
    "The  shared library we will build consist of a single source file: \n",
    "\n",
    "* `statistics.c/h`\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting ./include/statistics.h\n"
     ]
    }
   ],
   "source": [
    "%%file ./include/statistics.h\n",
    "#ifndef STATISTICS_H\n",
    "#define STATISTICS_H\n",
    "\n",
    "double  mean(double data[], int size);\n",
    "\n",
    "#endif\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting ./src/statistics.c\n"
     ]
    }
   ],
   "source": [
    "%%file ./src/statistics.c\n",
    "\n",
    "#include \"statistics.h\"\n",
    "\n",
    "double  mean(double data[], int size)\n",
    "{\n",
    "  /* Compute the arithmetic mean of a dataset using the recurrence relation \n",
    "     mean_(n) = mean(n-1) + (data[n] - mean(n-1))/(n+1)   */\n",
    "\n",
    "  double mean = 0;\n",
    "  for(int  i = 0; i < size; i++)\n",
    "  {\n",
    "      mean += (data[i] - mean) / (i + 1);\n",
    "   }\n",
    "  return mean;\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "### 1.2 The shared library\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Step 1: Compiling with Position Independent Code\n",
    "\n",
    "We compile the C file with `Position Independent Code( PIC )` \n",
    "\n",
    "\n",
    "* **`-fPIC`** : stands for `Position Independent Code`(位置无关代码)\n",
    "   \n",
    "   the generated machine code is `not dependent` on being located at a `specific address` in order to `work`.\n",
    "   \n",
    "   Position-independent code can be `executed` at `any memory address`\n",
    "     "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "!gcc -c -O3 -Wall -fPIC  -o  ./obj/statistics.o  ./src/statistics.c -I./include/"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* `-c`: compile into object file with default name : *.o.\n",
    "\n",
    "      By default, the object file has the same name as the source file with extension of \".o\" \n",
    "  \n",
    "  \n",
    "* `-O3`: Optimize yet more.\n",
    "\n",
    "      turns on all optimizations specified by -O2 and also turns on the -finline-functions, -fweb, -frename-registers and -funswitch-loops optionsturns  on  all  optimizations   \n",
    "     \n",
    "  \n",
    "* `-Wall`: prints \"all\"  compiler's warning message. \n",
    "\n",
    "      This option should always be used, in order to generate better code."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Step 2: Creating a shared library from an object file\n",
    "\n",
    "* -shared : creating a shared library"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "!gcc -shared -o  ./bin/libstat.dll ./obj/statistics.o"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Makefile of Building the shared library**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting ./makestatso.mk\n"
     ]
    }
   ],
   "source": [
    "%%file ./makestatso.mk\n",
    "\n",
    "CC=gcc\n",
    "CFLAGS=-O3 -Wall -fPIC \n",
    "\n",
    "SRC=./src/\n",
    "OBJ=./obj/\n",
    "BIN=./bin/\n",
    "INC=./include/\n",
    "\n",
    "# Windows\n",
    "LIB=libstat.dll\n",
    "\n",
    "all:  $(LIB)\n",
    "\n",
    "$(LIB): statobj\n",
    "\t $(CC) -shared -o $(BIN)$@ $(OBJ)statistics.o\n",
    "    \n",
    "statobj: $(SRC)statistics.c\n",
    "\t $(CC) -c $(CFLAGS)  -o $(OBJ)statistics.o $(SRC)statistics.c -I$(INC)    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "gcc -c -O3 -Wall -fPIC   -o ./obj/statistics.o ./src/statistics.c -I./include/    \n",
      "gcc -shared -o ./bin/libstat.dll ./obj/statistics.o\n"
     ]
    }
   ],
   "source": [
    "!make -f makestatso.mk"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 驱动器 J 中的卷是 cmh\n",
      " 卷的序列号是 9C25-3306\n",
      "\n",
      " J:\\SEU\\SEE\\PySEE\\home\\notebook\\demo\\bin 的目录\n",
      "\n",
      "2021/05/08  16:50           309,891 libstat.dll\n",
      "               1 个文件        309,891 字节\n",
      "               0 个目录 91,546,353,664 可用字节\n"
     ]
    }
   ],
   "source": [
    "!dir .\\bin\\libstat*.*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Step3: Using the shared library"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Header Files and Libraries \n",
    "\n",
    "* `Header File`: When compiling the program, the **compiler** needs the **header** files to compile the source codes;\n",
    "\n",
    "* `libraries`: the **linker** needs the **libraries** to resolve external references from other object files or libraries. \n",
    "\n",
    "The `compiler` and `linker` will not find the `headers/libraries` unless you set **the appropriate options**\n",
    "\n",
    "* **1 Searching for Header Files**\n",
    "\n",
    "   **`-Idir`:** The include-paths are specified via **-Idir** option (`uppercase` 'I' followed by the directory path. \n",
    "   \n",
    "   \n",
    "* **2 Searching for libraries Files**\n",
    "\n",
    "   **`-Ldir`**: The library-path is specified via **-Ldir** option (`uppercase` 'L' followed by the directory path）. \n",
    "\n",
    "\n",
    "* **3 Linking the library**\n",
    "\n",
    "   **`-lname`**: Link with the library name **without** the `lib` prefix and the extensions.\n",
    " \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We use the **same** code ` ./src/statdemo.c` to demonstrates calling the shared library \n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Makefile**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting ./makestatsodemo.mk\n"
     ]
    }
   ],
   "source": [
    "%%file ./makestatsodemo.mk\n",
    "\n",
    "SRC= ./src/\n",
    "OBJ= ./obj/\n",
    "BIN= ./bin/\n",
    "INC=./include/\n",
    "\n",
    "all: statdemoso\n",
    "\n",
    "statdemoso: statobj  \n",
    "\tgcc -o $(BIN)$@ $(OBJ)statdemo.o -L$(BIN) -lstat\n",
    "\n",
    "statobj: $(SRC)statdemo.c \n",
    "\tgcc -c -o $(OBJ)statdemo.o $(SRC)statdemo.c -I$(INC)\n",
    "    \n",
    "run:\n",
    "\t./bin/statdemoso  \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "gcc -c -o ./obj/statdemo.o ./src/statdemo.c -I./include/\n",
      "gcc -o ./bin/statdemoso ./obj/statdemo.o -L./bin/ -lstat\n"
     ]
    }
   ],
   "source": [
    "!make -f makestatsodemo.mk"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 驱动器 J 中的卷是 cmh\n",
      " 卷的序列号是 9C25-3306\n",
      "\n",
      " J:\\SEU\\SEE\\PySEE\\home\\notebook\\demo\\bin 的目录\n",
      "\n",
      "2021/05/08  16:06           330,645 statdemo.exe\n",
      "2021/05/08  16:53           330,674 statdemoso.exe\n",
      "               2 个文件        661,319 字节\n",
      "               0 个目录 91,546,349,568 可用字节\n"
     ]
    }
   ],
   "source": [
    "!dir .\\bin\\statdemo*.*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mean is 4.400000\n"
     ]
    }
   ],
   "source": [
    "!.\\bin\\statdemoso"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "./bin/statdemoso\n",
      "mean is 4.400000\n"
     ]
    }
   ],
   "source": [
    "!make -f makestatsodemo.mk run"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Step4 Automatic building all\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting ./makelibandclient.mk\n"
     ]
    }
   ],
   "source": [
    "%%file ./makelibandclient.mk\n",
    "all:\n",
    "\tmake -f makestatso.mk\n",
    "\tmake -f makestatsodemo.mk\n",
    "\tmake -f makestatsodemo.mk run"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "make -f makestatso.mk\n",
      "make[1]: Entering directory 'J:/SEU/SEE/PySEE/home/notebook/demo'\n",
      "gcc -c -O3 -Wall -fPIC   -o ./obj/statistics.o ./src/statistics.c -I./include/    \n",
      "gcc -shared -o ./bin/libstat.dll ./obj/statistics.o\n",
      "make[1]: Leaving directory 'J:/SEU/SEE/PySEE/home/notebook/demo'\n",
      "make -f makestatsodemo.mk\n",
      "make[1]: Entering directory 'J:/SEU/SEE/PySEE/home/notebook/demo'\n",
      "gcc -c -o ./obj/statdemo.o ./src/statdemo.c -I./include/\n",
      "gcc -o ./bin/statdemoso ./obj/statdemo.o -L./bin/ -lstat\n",
      "make[1]: Leaving directory 'J:/SEU/SEE/PySEE/home/notebook/demo'\n",
      "make -f makestatsodemo.mk run\n",
      "make[1]: Entering directory 'J:/SEU/SEE/PySEE/home/notebook/demo'\n",
      "./bin/statdemoso  \n",
      "mean is 4.400000\n",
      "make[1]: Leaving directory 'J:/SEU/SEE/PySEE/home/notebook/demo'\n"
     ]
    }
   ],
   "source": [
    "!make -f makelibandclient.mk"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2 Building  a Shared Library with Multi-source\n",
    "\n",
    "### 2.1  The multi-source files\n",
    "The shared library we will build consist of the multi-source files\n",
    "\n",
    "* statistics.c/h\n",
    "\n",
    "* number.c/h\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting ./include/number.h\n"
     ]
    }
   ],
   "source": [
    "%%file ./include/number.h\n",
    "\n",
    "#ifndef NUMBER_H\n",
    "#define NUMBER_H\n",
    "\n",
    "\n",
    "#ifdef __cplusplus\n",
    "extern \"C\"\n",
    "{\n",
    "#endif\n",
    "int factorial(int n);\n",
    "\n",
    "\n",
    "#ifdef __cplusplus\n",
    "}\n",
    "#endif\n",
    "\n",
    "#endif"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting ./src/number.c\n"
     ]
    }
   ],
   "source": [
    "%%file ./src/number.c\n",
    "\n",
    "#include \"number.h\"\n",
    "\n",
    "// The factorial of a positive integer n, denoted by n!, \n",
    "//    is the product of all positive integers less than or equal to n. \n",
    "//  For example,5!=5*4*3*2*1=120\n",
    "//  The value of 0! is 1 \n",
    "int factorial(int n)\n",
    "{\n",
    "    if (n == 0 ) {\n",
    "        return 1;\n",
    "    }\n",
    "    else \n",
    "    {\n",
    "        return n * factorial(n - 1);\n",
    "    }\n",
    "}\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.2 Building  into One Shared Library\n",
    "\n",
    "**Makefile**\n",
    "\n",
    "```make\n",
    "SRCS=$(SRC)statistics.c \\\n",
    "    $(SRC)bubblesort.c \n",
    "```    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting ./makelibmaths.mk\n"
     ]
    }
   ],
   "source": [
    "%%file ./makelibmaths.mk\n",
    "\n",
    "CC=gcc\n",
    "CFLAGS=-O3 -Wall -fPIC\n",
    "\n",
    "SRC= ./src/\n",
    "OBJ= ./obj/\n",
    "BIN= ./bin/\n",
    "INC= ./include/\n",
    "\n",
    "LIB=libmaths.dll\n",
    "\n",
    "SRCS=$(SRC)statistics.c \\\n",
    "    $(SRC)number.c \n",
    "\n",
    "# non-path filename\n",
    "filename=$(notdir $(SRCS))\n",
    "\n",
    "# the obj target of a source code using the pattern rule\n",
    "OBJS=$(patsubst %.c,$(OBJ)%.o,$(filename))\n",
    "\n",
    "all:$(LIB)\n",
    "    \n",
    "$(LIB): $(OBJS)  \n",
    "\t$(CC) -shared -o $(BIN)$@ $^ \n",
    "\n",
    "# the pattern rule: one step rule for multiple source files\n",
    "$(OBJS):$(SRCS)\n",
    "\t$(CC) $(CFLAGS) -o $@ -c $(SRC)$(patsubst  %.o,%.c,$(notdir $@))  -I$(INC)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "gcc -shared -o ./bin/libmaths.dll obj/statistics.o obj/number.o \n"
     ]
    }
   ],
   "source": [
    "!make -f makelibmaths.mk"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 驱动器 J 中的卷是 cmh\n",
      " 卷的序列号是 9C25-3306\n",
      "\n",
      " J:\\SEU\\SEE\\PySEE\\home\\notebook\\demo\\bin 的目录\n",
      "\n",
      "2021/05/08  17:03           310,189 libmaths.dll\n",
      "               1 个文件        310,189 字节\n",
      "               0 个目录 91,546,349,568 可用字节\n"
     ]
    }
   ],
   "source": [
    "!dir .\\bin\\libm*.dll"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.3 Building a client executable \n",
    "\n",
    "The code demonstrates calling the shared library's functions:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting ./src/demomath.c\n"
     ]
    }
   ],
   "source": [
    "%%file ./src/demomath.c\n",
    "\n",
    "#include <stdio.h> \n",
    "#include \"statistics.h\"\n",
    "#include \"number.h\"\n",
    "\n",
    "int main() {\n",
    "    \n",
    "     double a[] = {8, 4, 5, 3, 2};\n",
    "     int len= sizeof(a)/sizeof(*a);\n",
    "     printf(\"mena is %f\\n\", mean(a,  len)); \n",
    "    \n",
    "     int n =5;\n",
    "     printf(\"the factorial of  %d is %d\\n\",n,factorial(n));  // 5!=120\n",
    "     return 0;\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "!gcc -o  ./bin/demomath ./src/demomath.c  -L./bin/ -lmaths -I./include"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mena is 4.400000\n",
      "the factorial of  5 is 120\n"
     ]
    }
   ],
   "source": [
    "!.\\bin\\demomath"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3 The C/C++ Caller of CoolProp Library in Windows\n",
    "\n",
    "**Change working dir to `./notebook/coolpropdemo`**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'J:\\\\SEU\\\\SEE\\\\PySEE\\\\home\\\\notebook'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%pwd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "J:\\SEU\\SEE\\PySEE\\home\\notebook\\coolpropdemo\n"
     ]
    }
   ],
   "source": [
    "%cd ./coolpropdemo"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "* [Official Doc: CoolProp Shared Library](http://www.coolprop.org/coolprop/wrappers/SharedLibrary/index.html#)\n",
    "\n",
    "**The Windows Shared Library**\n",
    "\n",
    "1. The official Windows Shared Library: `CoolProp_x64.dll`\n",
    "\n",
    "2. The Shared Library builded with TDM-GCC-64: `libCoolProp.dll`\n",
    "\n",
    "The example use a TDM-GCC-64's dll: **libCoolProp.dll** \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### C Caller\n",
    "\n",
    "* -DCOOLPROP_LIB "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting ./main.c\n"
     ]
    }
   ],
   "source": [
    "%%file ./main.c\n",
    "\n",
    "/* \n",
    "The example use the shared library of CoolProp in c\n",
    "\n",
    "g++ -DCOOLPROP_LIB -o ./bin/main   main.c -I./include -L./bin -lCoolProp\n",
    "./bin/main\n",
    "\n",
    "*/\n",
    "\n",
    "#include \"CoolPropLib.h\"\n",
    "#include <stdio.h>\n",
    "\n",
    "int main() {\n",
    "     double value=PropsSI(\"P\",\"T\",273.15+0,\"Q\",0,\"R134a\");\n",
    "     printf(\"%f\\n\", value); \n",
    "     return 0;\n",
    "}\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Compiling"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "!g++  -DCOOLPROP_LIB -o ./bin/main  main.c -I./include -L./bin -lCoolProp"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Run"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "292803.182339\n"
     ]
    }
   ],
   "source": [
    "!.\\bin\\main"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### C++ Caller"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting ./demo.cpp\n"
     ]
    }
   ],
   "source": [
    "%%file ./demo.cpp\n",
    "/* \n",
    "The example use the shared library of CoolProp in c++\n",
    "\n",
    "g++ -DCOOLPROP_LIB -o ./bin/demo  demo.cpp -I./include -L./bin -lCoolProp\n",
    "\n",
    "./bin/demo\n",
    "\n",
    "*/\n",
    "\n",
    "#include \"CoolPropLib.h\"\n",
    "#include <iostream>\n",
    "#include <iomanip>\n",
    "\n",
    "int main()\n",
    "{\n",
    "    double value=PropsSI(\"P\",\"T\",273.15+0,\"Q\",0,\"R134a\");\n",
    "    std::cout << std::setiosflags(std::ios::fixed)  << value << std::endl;\n",
    "    return 1;\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Compiling"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "!g++ -DCOOLPROP_LIB -o ./bin/demo  demo.cpp -I./include -L./bin -lCoolProp"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Run"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "292803.182339\n"
     ]
    }
   ],
   "source": [
    "!.\\bin\\demo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.7"
  },
  "latex_envs": {
   "LaTeX_envs_menu_present": true,
   "autoclose": false,
   "autocomplete": true,
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 1,
   "hotkeys": {
    "equation": "Ctrl-E",
    "itemize": "Ctrl-I"
   },
   "labels_anchors": false,
   "latex_user_defs": false,
   "report_style_numbering": false,
   "user_envs_cfg": false
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "165px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  },
  "widgets": {
   "state": {},
   "version": "1.1.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}